<? 
// Page variables
$rightCol   = 1;
$parseURI   = 1;
$loggedUser = 0;
$ads        = 1;

include("includes/functions.php"); 
// Check for q
if (!empty($_POST[q])) { $q = $_POST[q]; }
elseif (!empty($_GET[q])) { $q = $_GET[q]; }
if ($_GET[newSearch] == 1) {
	$_SESSION[searchRel] = null;
}

$searchTerm = urldecode($q);
$searchTerm = fieldToDB($searchTerm);
$results = 0;
if ( (!empty($searchTerm)) && ($searchTerm != 'Search the network...') ) {
	$timeStart = microtime_float();
	include("includes/search/searchResources.php");	
	$timeEnd = microtime_float();	
	$timeElapsed = ($timeEnd-$timeStart)*1000;
	$timeElapsedDisplay = round($timeElapsed,1);
		
	// Add dbase entries
	$sqlE1 = "UPDATE transVisitorStats SET DidSearch = '1' WHERE SID = '".session_id()."'";
	$resultE1 = mysql_db_query($dbname,$sqlE1);
	
	if (empty($page)) {
		// We don't want to log this for every search page that's returned
		$sqlE2 = "INSERT into transVisitorSearch (
				SID,
				SearchTerm,
				NoResults,
				SearchTime,
				SearchDate
				) values (
				'".session_id()."',
				'$searchTerm',
				'$results',
				'$timeElapsed',
				'".date('Y-m-d')."'
				)";
		$resultE2 = mysql_db_query($dbname,$sqlE2);
	}
}
// Remove slashes from searchTerm
$searchTerm = str_replace(Chr(92), '', $searchTerm);
include("includes/header.php"); ?>

	<div id="pageContent">
		<img src="images/page_top.gif" title=""><br>	
		<? include("includes/menu_main.php"); ?>
		<? if ($rightCol == 1) { ?>
		<div id="rightCol">			
			<img src="images/rightColTop.gif">
			<? include("includes/rightCol.php"); ?>			
		</div>
		<? } ?>
		<div id="mainContent">
			<h1>Search the Network?</h1><br>
			<p class="margin">
						
			<? if ( (empty($searchTerm)) || ($searchTerm == "Search again") ) { ?>
				<br>
				<b>Your search term was empty.</b><br>
				<form action="<?=$PHP_SELF; ?>" method="POST" name="searchForm">
				<input type="text" maxlength="50" name="q" size="30" value="Search again" onFocus="searchForm.q.value='';">
				<input type="submit" value="Search">
				</form>
				<div class="spacer2"></div>
			<? } elseif ($results == 0) { ?>
			<!-- Display no results -->
			<br>
				<b><font color="red">Sorry!</font></b> Your search for <b><?=$searchTerm; ?></b> returned no results.<br>
				<form action="<?=$PHP_SELF; ?>" method="POST" name="searchForm">
				<input type="text" maxlength="50" name="q" size="30" value="Search again" onFocus="searchForm.q.value='';">
				<input type="submit" value="Search">
				</form>
				<div class="spacer2"></div>
			
			<? } else { 
				echo "<img src=\"images/bullet_red.gif\"> There are <b>$totalResults</b> results for <b>$searchTerm</b> 
				[$timeElapsedDisplay mS]\n";
				// Display returned resources
				if ($resultsResources == 1) {
					echo "<div class=\"headBar\">Matching Resources</div>\n";
					if ($resourcePages > 1) {
						$q = urlencode($q);
						$urlString = "q=".$q;
						paginate($resourcePages,20,$urlString,$page,'');						
					}
					echo "<table border=\"0\">\n";
					
					$rElements = count($linkIDArray);
					$linkIDArrayCheck = array();					
					for($a=0;$a<=($rElements-1);$a++) {
						$dontDisplay = 0;
						$linkID     = $linkIDArray[$a];
						$linkPath   = $pathArray[$a];
						$linkTitle  = $titleArray[$a];
						$linkDesc   = $descArray[$a];
						$link       = $linkArray[$a];
						$linkDate   = $dateArray[$a];
						$relPercent = $relPerArray[$a];
						$linkViews  = $viewsArray[$a];
						
						if ($linkViews == 1) { $linkViewsText = 'View'; }
						else { $linkViewsText = 'Views'; }
																	
						echo "<tr>\n";
						echo "<td valign=\"top\">";
						if ($relPercent > 0) {
							echo "$relPercent%\n";
						}
						echo "</td>\n";
						echo "<td><div style=\"padding: 4px;\" class=\"linkLink\"><a href=\"$siteURL/linkViewPopup.php?linkID=$linkID\" target=\"_blank\">$linkTitle</a></div>\n";
						if ( ($linkDate != "0000-00-00") && (!empty($linkDate)) ) { 
							echo "<font color=\"#CC6600\"><b>";
							mysql_to_date($linkDate);
							echo "</b></font> &nbsp;&nbsp;<img src=\"images/icon_small_view.gif\" alt=\"$linkViews $linkViewsText\"> <span class=smallGray>$linkViews $linkViewsText</span><br>";
						}	
						if (!empty($linkDesc)) { echo "<br />".$linkDesc; } 
						echo "<br><br></td></tr>\n";
					}
					echo "</table>\n";				
				}
			} ?>
			<?
			if ($resourcePages > 1) {
				$q = urlencode($q);
				$urlString = "q=".$q;
				paginate($resourcePages,20,$urlString,$page,'');						
			}
			?>
		</p>	
		</div>
		<? if ($rElements<10) { echo "<div class=\"spacer2\"></div>\n"; } ?>
	</div> <!-- End Page Content -->
<? include("includes/footer.php"); ?>